<template>
  <div class="container tree-list-layout">
    <treeOffice @click="handleTreeNodeClick" ref="treeOffice" class="box-left" />
    <data-view
      class="box-right"
      ref="dataView"
      propViewKey="base_reagent_team"
      propViewForm="base/reagentteam/detail.vue"
      :propFlowKey="propFlowKey"
      @extra-button-click="handleExtraButtonClick"
      :propInitData="selectedTreeNode"
      :propIsTdesign="true"
    />
  </div>
</template>

<script>
import treeOffice from '@/components/custom/bus/treeOffice.vue'

export default {
  name: 'reagentteam',
  data() {
    return {
      dialogUserVisible: false,
      defaultProps: {
        children: 'children',
        label: 'label',
      },
      selectedTreeNode: {},
      deptInitData: {},
    }
  },
  components: {
    treeOffice,
  },
  created() {},
  methods: {
    handleExtraButtonClick(para) {
      if (para.button.key === 'show-user') {
        this.deptInitData = para.data
        this.dialogUserVisible = true
        this.$nextTick(() => {
          let wrapper = this.$newWrapper().eq('reagentTeamId', this.deptInitData.id)
          this.$refs.dvUser.setQueryWrapper(wrapper)
        })
      } else if (para.button.key === 'sync-user') {
        this.$ajax.get('base/reagent_team/syncUser').then((response) => {
          if (response.code == 0) {
            this.utilMessage.success(`数据同步成功，影响记录数量:${response.data}`)
            this.$refs.dataView.ocRefresh()
          }
        })
      }
    },
    handleTreeNodeClick(data) {
      this.selectedTreeNode = data
      let wrapper = data.id == 0 ? this.$newWrapper() : this.$newWrapper().or('dept_id', data.id) //包含自己
      this.$refs.dataView.setQueryWrapper(wrapper)
    },
    extraButtonClick({ button, data, index }) {
      if (button.key === 'closeForm') {
        this.dialogUserVisible = false
      }
    },
    // 查找父级节点
    findParentNode(tree, node) {
      if (tree.id === node.id) {
        return true
      } else if (!tree.children) {
        return false
      } else {
        if (tree.children.find((t) => this.findParentNode(t, node))) {
          node.parent = { id: tree.id, label: tree.label }
        }
      }
    },
    //行编辑选择事件
    async selectChange({ rows, column }) {
      //多选，已存在的user自动更新
      if (column.property == 'code') {
        if (rows) {
          this.$ajax
            .post(`base/reagent_team_user/saveUser?reagentTeamId=${this.deptInitData.id}`, rows)
            .then((response) => {
              if (response.code == 0) {
                this.utilMessage.success(
                  `保存成功，新增：${response.data.addCount}条记录，更新：${response.data.updateCount}条记录`
                )
                this.$refs.dvUser.ocRefresh()
              }
            })
        }
      }
    },
    getUserParam(param) {
      let wrapper = this.$newWrapper()
      wrapper.eq('office_id', this.deptInitData.deptId)
      //固定数据筛选参数
      let userParam = {
        checkType: 'checkbox',
        viewKey: 'base_staff_reagent_select',
        //筛选耗材的sql语句
      }
      param.call({ userParam, editQueries: wrapper })
    },
  },
  props: ['propFlowKey'],
}
</script>
